package priv.bestbeat.cloud.api.gateway.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * BCryptPasswordEncoder 工具类
 * @author 张渠钦
 * @date 2021/12/31 17:09
 */
@Slf4j
public final class BCryptPasswordUtil {

    private static BCryptPasswordEncoder encoder;

    static {
        try {
            encoder = new BCryptPasswordEncoder(
                    BCryptPasswordEncoder.BCryptVersion.$2A,
                    31,
                    SecureRandom.getInstance("SHA1PRNG")
            );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static boolean match(String authentication,String password) {
        return encoder.matches(authentication,password);
    }

    public static String encode(String authentication) {
        return encoder.encode(authentication);
    }
}
